package com.fenghongzhang.day02;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Toast;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class Main3Activity extends AppCompatActivity {

    private long time = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        //如果是回退键
        if(keyCode == KeyEvent.KEYCODE_BACK){
            if(System.currentTimeMillis() - time > 2000 ){ //191219129111
                Toast.makeText(this, "在按就推出", Toast.LENGTH_SHORT).show();
                time = System.currentTimeMillis();
                return false;
            }else{
                finish();
            }
        }
        return super.onKeyDown(keyCode, event);
    }

    public void down(View view) {
        new MyTask().execute("http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=20&page=1");
    }


    /**
     * 1,参数, --> 网址
     * 2.进度 有->Integer 没有- Void
     * 3,返回值 -> 子线程的返回值.
     *   参数- > 主线程的参数
     */
    class MyTask extends AsyncTask<String,Void,String>{
        /**
         * 子线程
         * @param strings
         * @return
         */
        @Override
        protected String doInBackground(String... strings) {
            try {
                URL url = new URL(strings[0]);
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//                connection.setReadTimeout(1000);

                if (connection.getResponseCode() == 200){
                    InputStream inputStream = connection.getInputStream();
                    StringBuilder stringBuilder = new StringBuilder();
                    byte[] bytes = new byte[1024];
                    int len = 0;
                    while((len = inputStream.read(bytes)) != -1){
                        String s = new String(bytes, 0, len);
                        stringBuilder.append(s);
                    }

                    String string = stringBuilder.toString();
                    return string;
                }
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return "";
        }

        /**
         * 主线程
         * @param s
         */
        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            Toast.makeText(Main3Activity.this, s, Toast.LENGTH_SHORT).show();
        }
    }

    class MyTask2 extends AsyncTask<String,Void, Bitmap>{

        @Override
        protected Bitmap doInBackground(String... strings) {
            return null;
        }

        @Override
        protected void onPostExecute(Bitmap bitmap) {
            super.onPostExecute(bitmap);
            //给组件赋值
        }

        @Override
        protected void onProgressUpdate(Void... values) {
            super.onProgressUpdate(values);
        }
    }

    class MyTask3 extends AsyncTask<String,Integer,Bitmap>{

        @Override
        protected Bitmap doInBackground(String... strings) {
            publishProgress(11);
            return null;
        }

        @Override
        protected void onPostExecute(Bitmap bitmap) {
            super.onPostExecute(bitmap);
        }

        @Override
        protected void onProgressUpdate(Integer... values) {
            super.onProgressUpdate(values);

        }
    }



    class MyTask4 extends AsyncTask<String,Integer,String>{
        @Override
        protected String doInBackground(String... strings) {
            return null;
        }
    }
}
